package com.competition.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.front.entity.UserAdditional;
import com.competition.front.service.UserAdditionalService;
import com.competition.servicebase.exceptionhandler.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.competition.front.entity.Module;
import com.competition.front.mapper.ModuleMapper;
import com.competition.front.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 投稿信息表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-03-30
 */
@Service
public class ModuleServiceImpl extends ServiceImpl<ModuleMapper, Module> implements ModuleService {

    @Autowired
    private UserAdditionalService userAdditionalService;


    //根据模块id查看对应稿子
    @Override
    public List<Module> selectModuleById(String moduleId, Integer page, Integer limit) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("relationship_id",moduleId);
        //查询已通过稿子
        queryWrapper.eq("status",1);
        //分页
        Page<Module> pages=new Page<>(page,limit);
        IPage<Module> moduleIPage = baseMapper.selectPage(pages, queryWrapper);
        System.out.println(moduleIPage.getRecords().size());
        //展示分页数据
        List<Module> records = moduleIPage.getRecords();
        return records;
    }

    //根据用户id查看对应稿子
    @Override
    public List<Module> selectModuleByUserIdAndRelationId(String userId,String relationId) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        //查询所有对应稿子
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("relationship_id",relationId);
        List<Module> modules = baseMapper.selectList(queryWrapper);
        return modules;
    }
    //用户投稿
    @Override
    @Transactional
    public boolean saveModule(Module module) {
        String userId = module.getUserId();
        //插入稿子
        int insert = baseMapper.insert(module);
        QueryWrapper<UserAdditional> userAdditionalQueryWrapper=new QueryWrapper<>();
        userAdditionalQueryWrapper.eq("user_id",userId);
        List<UserAdditional> userAdditionalList = userAdditionalService.list(userAdditionalQueryWrapper);
        if(userAdditionalList.size()==0){
            UserAdditional userAdditional=new UserAdditional();
            userAdditional.setUserId(userId);
            userAdditionalService.adduserAdditional(userAdditional);
        }else if(userAdditionalList.size()==1){
            Integer essayNumber = userAdditionalList.get(0).getEssayNumber();
            essayNumber=essayNumber+1;
            userAdditionalList.get(0).setEssayNumber(essayNumber);
            UpdateWrapper<UserAdditional> userAdditionalUpdateWrapper=new UpdateWrapper<>();
            userAdditionalUpdateWrapper.eq("user_id",userAdditionalList.get(0).getUserId());
            userAdditionalService.update(userAdditionalList.get(0),userAdditionalUpdateWrapper);
        }else{
            throw new MyException(201,"插入失败");
        }
        if(insert!=1) throw new MyException(201,"插入失败");
        return true;
    }

    @Override
    public Integer countModuleByRelationId(String relation) {
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("relationship_id",relation);
        queryWrapper.eq("status",1);
        return baseMapper.selectCount(queryWrapper);
    }

}
